﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using JKF.DataModule.IDAL;
using JKF.Substructure;
using System.Data.SqlClient;//请先添加引用

namespace JKF.DataModule.SqlServerDAL
{
    public partial class dalSYS_LOG : IdalSYS_LOG
    {
        private const string FieldShow = "LOG_KEY,MODULE_NAME,TABLE_NAME,PRIMARY_KEY,LOG_CONTENT,OPERATION_TYPE,IP,BROWSER,DATE_CREATE,ACCOUNT";
        private const string FieldOrder = "DATE_CREATE desc";
        private const string FieldKey = "LOG_KEY";

        public DataTable LogSearch(string searchItem, string matchWay, string keyword, string DATE_START, string DATE_END,
         string moduleName, string userKey, int pageCurrent, int pageSize, ref int pageCount, ref int rowCount)
        {
            var sql = new StringBuilder(@"  1=1  ");
            sql.AppendFormat(" and exists(select shop_code from sys_user_shop  where V_SYS_LOG.shop_code=sys_user_shop.shop_code  and sys_user_shop.USER_KEY='{0}')", userKey);
            if (!string.IsNullOrEmpty(searchItem) && !string.IsNullOrEmpty(keyword))
            {
                if (matchWay == "包含")
                {
                    sql.AppendFormat(" AND {0} LIKE '%{1}%' ", searchItem, keyword);
                }
                else
                {
                    sql.AppendFormat(" AND {0} = '{1}' ", searchItem, keyword);
                }
            }

            if (!string.IsNullOrEmpty(moduleName))
            {
                sql.AppendFormat(" and MODULE_NAME ='{0}'",moduleName);
            }

            if (!string.IsNullOrEmpty(DATE_START))
            {
                sql.AppendFormat(" and DATE_CREATE >'{0}'", DATE_START);
            }
            if (!string.IsNullOrEmpty(DATE_END))
            {
                sql.AppendFormat(" and DATE_CREATE <'{0}'", DATE_END);
            }

            return DataPage.GetPageInfo("V_SYS_LOG", FieldKey, pageCurrent, pageSize, FieldShow, FieldOrder, sql.ToString(), ref pageCount, ref rowCount).Tables[0];
        }
    }
}
